<?php
//include("header.php");
include("includes/classes/Galerie.php");

if (empty($_GET['action'])) {
    $text->viewPart = "galerie";
    $smarty->assign("galerie_text", $text->getText());

    $folderArray = array();
    $tempFh = opendir($url_path . "galerie/");
    while(false !== ($folder = readdir($tempFh))) {
        if (substr($folder, 0, 1) != "." && substr($folder, 0, 1) != ".." && is_dir($url_path . "galerie/" . $folder))
            array_push($folderArray, $folder);
    }
    closedir($tempFh);
    foreach ($folderArray as $folderArrayIndex => $folder) {
        $pictures = glob($url_path . "galerie/" . $folder . "/thumbs/*");
        if (count($pictures) && $pictures[0])
            $picture_nr = rand(0, (count($pictures) - 1));
        else {
            $picture_nr = 0;
            $pictures[$picture_nr] = "picture_replace";
        }
        if (file_exists($url_path . "galerie/" . $folder . "/albumname.txt")) {
            $arr_name = file($url_path . "galerie/" . $folder  . "/albumname.txt");
            $album_name = trim(implode($arr_name));
        }
        $albums[$folderArrayIndex]['album_name'] = $album_name;
        $albums[$folderArrayIndex]['folder_name'] = $folder;
        $picArray = explode("/", $pictures[$picture_nr]);
        $picSize = sizeof($picArray);
        $albums[$folderArrayIndex]['random_picture'] = $picArray[$picSize-1];
    }
    $smarty->assign("albums", $albums);
    $smarty->assign("inc_tpl", "galerie/overview.tpl");
}

if ($_GET['action'] == "showalbum") {
    $album_folder = $_GET['album'];
    $smarty->assign("error", false);
    $smarty->assign("isset_album", true);
    if (!file_exists($url_path . "galerie/" . $album_folder)) {
        $smarty->assign("isset_album", false);
    } else {
        if (file_exists($url_path . "galerie/" . $album_folder . "/albumname.txt")) {
            $arr_name = file($url_path . "galerie/" . $album_folder . "/albumname.txt");
            $album_name = trim(implode($arr_name));

            $pictures = array();
            $tempFh = opendir($url_path . "galerie/" . $album_folder . "/thumbs");
            while(false !== ($picture = readdir($tempFh))) {
                if (substr($picture, 0, 1) != "." && substr($picture, 0, 1) != "..")
                    array_push($pictures, $picture);
            }
            closedir($tempFh);
            if (sizeof($pictures) == 0) {
                $pictures[0] = "picture_replace";
            }

            $smarty->assign("album_folder", $album_folder);
            $smarty->assign("album_name", $album_name);
            $smarty->assign("pictures", $pictures);
        } else
            $smarty->assign("error", true);
        $smarty->assign("inc_tpl", "galerie/album.tpl");
    }
}

if ($_GET['action'] == "fileupload") {
    $galerie = new Galerie;
    $galerie->url_path = $url_path;
    $galerie->picture = $_FILES['bild'];
    $galerie->album_folder = $_GET['album'];
    $result = $galerie->uploadFile();
    $upload = ($result[0]) ? "true" : "false";
    $reason = ($result[1]) ? $result[1] : "";
    header("location: " . $url_page  . "/index.php?site=galerie&action=showalbum&upload=" . $upload . "&album=" . $_GET['album'] . "&reason=" . $reason);
}

if ($_GET['action'] == "updateAlbumName") {
    $fp = fopen($url_path . "/galerie/" . $_GET['album'] . "/albumname.txt", "w");
    if (fwrite($fp, htmlentities($_POST['albumName'], ENT_QUOTES, 'UTF-8'))) {
        #header("location:" . $_GET['redirect_url'] . "&album=" . $_GET['album'] . "&update_albumName=true");
        #print 'true';
    } else {
        #print 'false';
        #header("location:" . $_GET['redirect_url'] . "&album=" . $_GET['album'] . "&update_albumName=false");
    }
    fclose($fp);
}

if ($_GET['action'] == "delete_pic") {
    if (unlink(HOME_PATH . "/galerie/" . $_GET['album'] . "/thumbs/" . $_GET['pic']) && @unlink(HOME_PATH . "/galerie/" . $_GET['album'] . "/big/" . $_GET['pic']))
        header("location:" . HOME_URL . "?site=galerie&action=showalbum&album=" . $_GET['album'] . "&delete_pic=true");
}

if ($_GET['action'] == "delete_album") {
    if (removeDir($url_path . "/galerie/" . $_GET['album_name']))
        header("location:" . HOME_URL . "?site=galerie&delete_album=true");
    else
        header("location:" . HOME_URL . "?site=galerie&delete_album=false");
}

if ($_GET['action'] == "new_album") {
    $created = FALSE;
    ## Galerie-(Kategorie)-ordner installieren ##
    $_GET['album_name'] = rawurldecode($_GET['album_name']);
    $new_folder_name = strtolower(str_replace(
        Array("&auml;", "&uuml;", "&ouml;", "&szlig;", "&Auml;", "&Uuml;", "&Ouml;", " "),
        Array("ae","ue","oe","ss","Ae","Ue","Oe", "_"),
        htmlentities($_GET['album_name'], ENT_QUOTES, 'UTF-8')
    ));
    if (mkdir($url_path . "/galerie/" . $new_folder_name)) {
        chmod($url_path . "/galerie/" . $new_folder_name, 0777);
        $created = TRUE;
    } else
        header("location:" . HOME_URL . "?site=galerie&new_album=false");
    ## Unterordner "big" unter Kategorieordner erstellen ##
    if (mkdir($url_path . "/galerie/" . $new_folder_name . "/big")) {
        chmod($url_path . "/galerie/" . $new_folder_name . "/big", 0777);
        $created = TRUE;
    } else {
        removeDir($url_path . "/galerie/" . $new_folder_name);
        header("location:" . HOME_URL . "?site=galerie&new_album=false");
    }
    ## Unterordner "thumbs" unter Kategorieordner erstellen ##
    if (mkdir($url_path . "/galerie/" . $new_folder_name . "/thumbs")) {
        chmod($url_path . "/galerie/" . $new_folder_name . "/thumbs", 0777);
        $created = TRUE;
    } else {
        removeDir($url_path . "/galerie/" . $new_folder_name);
        header("location:" . HOME_URL . "?site=galerie&new_album=false");
    }

    $fp = fopen($url_path . "/galerie/" . $new_folder_name . "/albumname.txt","w");
    if (fwrite($fp, htmlentities($_GET['album_name'], ENT_QUOTES, 'UTF-8')))
        $created = TRUE;
    else {
        removeDir($url_path . "/galerie/" . $new_folder_name);
        header("location:" . HOME_URL . "?site=galerie&new_album=false");
    }

    fclose($fp);
    if ($created)
        header("location:" . HOME_URL . "?site=galerie&action=showalbum&album=" . $new_folder_name . "&new_album=true");
}

$smarty->display("galerie/main.tpl");

//include("footer.php");
?>